from manim import *

class AreaDemo(Scene):
    def construct(self):
        ax = Axes(
            x_range=[0, 3],
            y_range=[0, 2],
            x_length=6,
            y_length=4,
            axis_config={"color": GREEN},
        )

        # 绘制矩形
        rectangle = Rectangle(width=3, height=2, fill_opacity=0.3, fill_color=BLUE)
        rectangle.move_to(ax.c2p(0, 0))

        self.add(ax, rectangle)

        # 标注长度和宽度
        text_width = Text("Width = 3", font_size=24).next_to(rectangle, DOWN)
        text_height = Text("Height = 2", font_size=24).next_to(rectangle, LEFT)

        self.add(text_width, text_height)

# gpt
class RectangleArea(Scene):
    def construct(self):
        # 创建坐标轴
        axes = Axes(
            x_range=[-1, 5, 1], 
            y_range=[-1, 4, 1], 
            axis_config={"color": BLUE}
        )
        
        # 标记坐标轴
        labels = axes.get_axis_labels(x_label="x", y_label="y")
        
        # 创建矩形
        rectangle = Polygon(
            axes.c2p(0, 0), 
            axes.c2p(3, 0), 
            axes.c2p(3, 2), 
            axes.c2p(0, 2),
            color=YELLOW
        )
        
        # 创建宽和高的标记
        width_brace = Brace(rectangle, DOWN, buff=SMALL_BUFF)
        width_text = width_brace.get_text("3")
        height_brace = Brace(rectangle, LEFT, buff=SMALL_BUFF)
        height_text = height_brace.get_text("2")

        # 创建面积的文字
        area_text = MathTex("3 \\times 2 = 6").move_to(axes.c2p(1.5, 2.5))
        
        # 将元素添加到场景中
        self.play(Create(axes), Write(labels))
        self.wait(1)
        self.play(Create(rectangle))
        self.wait(1)
        self.play(
            Create(width_brace),
            Write(width_text),
            Create(height_brace),
            Write(height_text)
        )
        self.wait(1)
        self.play(Write(area_text))
        
        # 保持一段时间
        self.wait(2)

# 如果你想直接通过命令行运行这个脚本，可以使用以下命令：
# manim -pql rectangle_area.py RectangleArea


class DrawSquare(Scene):
    def construct(self):
        # 创建坐标轴
        axes = Axes(
            x_range=[-1, 2, 1], 
            y_range=[-1, 2, 1], 
            axis_config={"color": BLUE}
        )
        
        # 标记坐标轴
        labels = axes.get_axis_labels(x_label="x", y_label="y")
        
       # 创建正方形
        square = Polygon(
            [0, 0, 0], 
            [1, 0, 0], 
            [1, 1, 0], 
            [0, 1, 0],
            color=YELLOW
        ).shift(axes.c2p(0, 0))
        


# 如果你想直接通过命令行运行这个脚本，可以使用以下命令：
# manim -pql square.py DrawSquare


class CoordsToPointExample(Scene):
    def construct(self):
        ax = Axes().add_coordinates()

        # a dot with respect to the axes
        dot_axes = Dot(ax.coords_to_point(2, 2), color=GREEN)
        lines = ax.get_lines_to_point(ax.c2p(2,2))

        # a dot with respect to the scene
        # the default plane corresponds to the coordinates of the scene.
        plane = NumberPlane()
        dot_scene = Dot((2,2,0), color=RED)

        self.add(plane, dot_scene, ax, dot_axes, lines)


class AxesDemo(Scene):
    def construct(self):
        ax = Axes(
            x_range=[-2, 2],
            y_range=[-2, 2],
            x_length=6,
            y_length=6,
            axis_config={"color": GREEN},
            x_axis_config={
                "numbers_to_include": np.arange(-2, 3, 1)
            },
        )
        self.add(ax)

        # 创建正方形
        square = Square(side_length=1).move_to(ax.coords_to_point(0, 0))
        self.add(square)

        









        